<template>
  <el-row>
    <el-col :span="24">
      <Portlet>
        <Create :visible.sync="visible" :id="rowId" :title="dialogTitle" @refresh="refresh"></Create>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersNo">
                <el-input v-model="search.manufacturersNo" placeholder="厂家编号"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersName">
                <el-input v-model="search.manufacturersName" placeholder="厂家名称"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersUserName">
                <el-input v-model="search.manufacturersUserName" placeholder="厂家联系人"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manufacturersUserPhone">
                <el-input v-model="search.manufacturersUserPhone" placeholder="厂家联系电话"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="state">
                <el-select v-model="search.state" placeholder="状态">
                  <el-option v-for="item in selectedState" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>

    <el-col :span="24">
      <basic-table
        ref="dataTable"
        url="/ms-warehouse-order/basicData/findManufacturersList"
        :columns="columns"
      >
        <template slot="column" >
          <el-table-column  fixed="right" align="center" width="100px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="changeState(scope.row)"
              >
                {{scope.row.state == 1 ? '停用': '启用'}}
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="changeMessage(scope.row)"
              >
                修改
              </el-button>
            </template>
          </el-table-column>
        </template>
        <el-button slot="buttons" type="primary" @click="create">新增</el-button>
      </basic-table>
    </el-col>
  </el-row>
</template>
<script>
import {dotData, copy} from '@/utils'
import {unixTimestampFilter} from '@/filters'
import {getInstallBusinessType, getInstallServiceType} from '@/utils/selectOptions'
import Create from '@/views/oms/basicData/manufactor/Create'
export default {
  name: 'basicDataManufactorIndex',
  components: {
    Create
  },
  data() {
    return {
      search: {
        manufacturersNo: '',
        manufacturersName: '',
        manufacturersUserName: '',
        manufacturersUserPhone: '',
        state: ''
      },
      businessArr: [],
      serviceArr: [],
      isOpen: false,
      searchLoading: false,
      costLoading: false,
      visible: false,
      rowId: '',
      dialogTitle: '',
      selectedState: [{value: '0', label: '停用'}, {value: '1', label: '启用'}],
      columns: [
        {
          label: '厂家编号',
          data: 'manufacturersNo'
        },
        {
          label: '厂家名称',
          data: 'manufacturersName'
        },
        {label: '厂家联系人', data: 'manufacturersUserName'},
        {label: '厂家联系电话', data: 'manufacturersUserPhone'},
        {
          label: '厂家地址',
          data: 'province+city+district',
          render(data, full) {
            return `${full.province} ${full.city} ${full.district}`
          }
        },
        {label: '详细地址', data: 'address'},
        {label: '操作人', data: 'founder'},
        {
          label: '添加时间',
          data: 'foundDate',
          render(data, type, full, meta) {
            return unixTimestampFilter(data)
          }
        },
        {label: '状态', data: 'state', render(data) { return data === 1 ? '启用' : '停用' }}
      ]
    }
  },
  created() {
    getInstallBusinessType().then(arr => {
      this.businessArr = arr
    })
    getInstallServiceType().then(arr => {
      this.serviceArr = arr
    })
  },
  methods: {
    searchFunc(search) {
      let data = copy(search, true)
      this.searchLoading = true
      this.$refs.dataTable.search(data).finally(_ => {
        this.searchLoading = false
      })
    },
    resetFields(searchForm) {
      this.$refs.searchForm.resetFields()
    },
    refresh() {
      this.searchFunc(this.search)
    },
    create() {
      // this.$router.push({path: '/ss-oms/basic-data/manufactor/create'})
      this.rowId = ''
      this.visible = true
      this.dialogTitle = '新增厂家信息'
    },
    show(row) {
      this.$router.push({path: '/ss-oms/basic-data/manufactor/show/' + row.id})
    },
    changeState(data) {
      this.costLoading = true
      if (data.state === 1) { // 执行停用
        this.$confirm('你确定停用该厂家吗?<br>停用厂家的同时会停用该厂家下所有的商品。', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          center: true,
          dangerouslyUseHTMLString: true,
          type: 'warning'
        }).then(() => {
          this.$ajax.post('/ms-warehouse-order/basicData/modifyStateManufacturers', {manufacturersId: data.id, state: 0}).then(response => {
            const success = dotData(response, 'success')
            if (success) {
              confirm('操作成功')
              this.refresh()
            }
            this.costLoading = false
          })
        }).catch(() => {
          this.costLoading = false
        })
      } else { // 执行启用
        this.$confirm('你确定启用该厂家吗?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          center: true,
          dangerouslyUseHTMLString: true,
          type: 'warning'
        }).then(() => {
          this.$ajax.post('/ms-warehouse-order/basicData/modifyStateManufacturers', {manufacturersId: data.id, state: 1}).then(response => {
            const success = dotData(response, 'success')
            if (success) {
              confirm('操作成功')
              this.refresh()
            }
            this.costLoading = false
          })
        }).catch(() => {
          this.costLoading = false
        })
      }
    },
    changeMessage(data) {
      if (data.id) {
        this.rowId = data.id
      } else {
        this.rowId = ''
      }
      this.visible = true
      this.dialogTitle = '修改厂家信息'
    }
  }
}
</script>
